var Mongoose = require('mongoose');

var TokenSchema = Mongoose.Schema({
    user: {
        type: Mongoose.Schema.ObjectId,
        ref: 'User'
    },
    token: {
        type: String
    }
});

var Token = Mongoose.model('Token', TokenSchema);

Token.createToken = function (newToken, callback) {
    newToken.save(callback);
};

Token.getUserByToken = function (token, callback) {
    Token.findOne({token: token}, callback).populate('user');
};

Token.deleteUserToken = function (token, callback) {
    Token.remove({token: token}, callback);
};

module.exports = Token;